package com.zhsj.OkeHomeConsumption.ui.activity.citypartner;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fantasy.doubledatepicker.DoubleDateSelectDialog;
import com.google.gson.Gson;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.CityzongsouruBean;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.LookpayeePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.LookpayeeUIInterFace;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   : 城市合伙人总收入
 */
public final class RevenueActivity extends MyActivity implements LookpayeeUIInterFace {

    @BindView(R.id.timer_text)
    TextView timerText;
    @BindView(R.id.timer_lin)
    LinearLayout timerLin;
    private LookpayeePresenter minePersonPresenter = null;
    @BindView(R.id.shenghuo_title)
    TextView shenghuoTitle;
    @BindView(R.id.shenghuo_recy)
    RecyclerView shenghuoRecy;
    @BindView(R.id.shenghuo_lin)
    LinearLayout shenghuoLin;
    @BindView(R.id.jiazhuang_title)
    TextView jiazhuangTitle;
    @BindView(R.id.jiazhuang_recy)
    RecyclerView jiazhuangRecy;
    @BindView(R.id.jiazahung_lin)
    LinearLayout jiazahungLin;
    private FormalUserInfo userinfo;
    private DoubleDateSelectDialog mDoubleTimeSelectDialog;

    private ZongshouruAdapter zongsourudapter;

    private OkeAdapter okeadapter;

    private List<CityzongsouruBean.LifeIncomeDTOListBean> shenghuo_list = null;

    private List<CityzongsouruBean.OkeIncomeDTOListBean> jiazhuang_list = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_revenue;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_copy_title;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        shenghuoRecy.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        shenghuoRecy.setNestedScrollingEnabled(false);//禁止滑动

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        //设置布局管理器
        jiazhuangRecy.setLayoutManager(layoutManager1);
        //设置为垂直布局，这也是默认的
        layoutManager1.setOrientation(OrientationHelper.VERTICAL);
        jiazhuangRecy.setNestedScrollingEnabled(false);//禁止滑动
    }

    @Override
    protected void initData() {
        shenghuo_list = new ArrayList<>();
        jiazhuang_list = new ArrayList<>();
        zongsourudapter = new ZongshouruAdapter();
        shenghuoRecy.setAdapter(zongsourudapter);

        okeadapter = new OkeAdapter();
        jiazhuangRecy.setAdapter(okeadapter);

        userinfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        minePersonPresenter = new LookpayeePresenter(getActivity(), this);
        minePersonPresenter.TotalrevenueGet(userinfo.getId(),"","");



        timerLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomTimePicker();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 获取城市合伙人总收入
     */
    @Override
    public void TotalrevenueGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");
                Gson gson = new Gson();
                CityzongsouruBean student2 = gson.fromJson(jsondata.toString(), CityzongsouruBean.class);
                shenghuo_list.clear();
                if (student2.getLifeIncomeDTOList().size() != 0) {
                    shenghuoLin.setVisibility(View.VISIBLE);
                    shenghuoTitle.setText("交易次数  " + student2.getLifeTotalTransactionNum() + " / 营业额  " + student2.getLifeTotalTurnover() + " / 收入  " + student2.getLifeTotalIncome());
                    shenghuo_list.addAll(student2.getLifeIncomeDTOList());
                    zongsourudapter.notifyDataSetChanged();
                } else {
                    shenghuoLin.setVisibility(View.GONE);
                }
                jiazhuang_list.clear();
                if (student2.getOkeIncomeDTOList().size() != 0) {
                    jiazahungLin.setVisibility(View.VISIBLE);
                    jiazhuangTitle.setText("家装订单  " + student2.getOkeTotalIncome() + " / 收入  " + student2.getOkeTotalTransactionNum());
                    jiazhuang_list.addAll(student2.getOkeIncomeDTOList());
                } else {
                    jiazahungLin.setVisibility(View.GONE);
                }

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取城市合伙人--- 总收入");
        }
    }


    public class ZongshouruAdapter extends BaseQuickAdapter<CityzongsouruBean.LifeIncomeDTOListBean, BaseViewHolder> {
        public ZongshouruAdapter() {
            super(R.layout.item_shenghuo, shenghuo_list);
        }

        @Override
        protected void convert(BaseViewHolder helper, final CityzongsouruBean.LifeIncomeDTOListBean item) {
            helper.setText(R.id.shenghuo_title, item.getShopName())
                    .setText(R.id.cishu_text, item.getTransactionNum() + "")
                    .setText(R.id.shouru_text, item.getIncome() + "")
                    .setText(R.id.price_text, item.getTurnover() + "");

        }
    }


    public class OkeAdapter extends BaseQuickAdapter<CityzongsouruBean.OkeIncomeDTOListBean, BaseViewHolder> {
        public OkeAdapter() {
            super(R.layout.item_jiazhuang, jiazhuang_list);
        }

        @Override
        protected void convert(BaseViewHolder helper, final CityzongsouruBean.OkeIncomeDTOListBean item) {
            helper.setText(R.id.name_text, item.getShopName())
                    .setText(R.id.dingdan_text, item.getTransactionNum() + "")
                    .setText(R.id.price_text, item.getIncome() + "");

        }
    }

    public void showCustomTimePicker() {

        if (mDoubleTimeSelectDialog == null) {
            mDoubleTimeSelectDialog = new DoubleDateSelectDialog(this, "", "", "");
            mDoubleTimeSelectDialog.setOnDateSelectFinished(new DoubleDateSelectDialog.OnDateSelectFinished() {
                @Override
                public void onSelectFinished(String startTime, String endTime) {
                    timerText.setText(startTime.replace("-", ".") + "-" + endTime.replace("-", "."));
                    minePersonPresenter.TotalrevenueGet(userinfo.getId(),startTime,endTime);
                }
            });

            mDoubleTimeSelectDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                }
            });
        }
        if (!mDoubleTimeSelectDialog.isShowing()) {
            mDoubleTimeSelectDialog.show();
        }
    }
}